
#include <sys/ioctl.h>
#include <sys/types.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include "config.h"
#include "font.h"
#include "prn_drv.h"



static prn_list_t gprn_list;

void prn_list_init(void)
{
	memset(&gprn_list, 0, sizeof(prn_list_t));
}
int prn_add_list(const uint8_t *buf, int size, int mode, void *param)
{
	int new_elem = 1;
	prn_mem_t *pelem;

	if (size + gprn_list.buflen > PRN_BUF_MAX_SIZE) return -1;

	if (gprn_list.memlen) {
		pelem = &gprn_list.prn_mem[gprn_list.memlen - 1];
		if (mode) {
			if (!memcmp(&pelem->inf.font, param,  sizeof(font_mode_t))) {
				new_elem = 0;
				
			}
		}
		else {
			
			if (!memcmp(&pelem->inf.image, param, sizeof(image_format_t))) {
				new_elem = 0;
			}
		}
	}

	if (new_elem) {
		if (gprn_list.memlen + 1 >= PRN_MEM_MAX_SIZE) return -1;
		pelem  = &gprn_list.prn_mem[gprn_list.memlen];
		pelem->mode = mode;
		if (mode) {//1: font data 0: image data
			memcpy(&pelem->inf.font, param, sizeof(font_mode_t));
		}
		else {
			memcpy(&pelem->inf.image, param, sizeof(image_format_t));
		}
		pelem->pdata = &gprn_list.buf[gprn_list.buflen];
		memcpy(pelem->pdata, buf, size);
		pelem->size = size;
		gprn_list.memlen ++;
	}
	else {
		memcpy(pelem->pdata + pelem->size, buf ,size);
		pelem->size += size;
	}
	gprn_list.buflen += size;
	//printf("memlen : %d\n", gprn_list.memlen);

	return 0;
}

int get_prn_memlen(void)
{
	return gprn_list.memlen;
}

prn_mem_t *get_prn_mem(int idx)
{
	if (idx >= gprn_list.memlen) {
		return NULL;
	}
	return &gprn_list.prn_mem[idx];
}



